// order-detail
import { PACKAGE_ACTIVITY_PAGE } from '../../../constants/route';
import { ORDER_TYPE } from '../../../constants/order';
import { handleBrandAfterSaleOrderStatus } from '../../biztools/order';
import { getBrandAfterSaleOrderDetail, dealBrandAfterSaleOrder, brandAgreeWearhouse,
    brandRefuseWearhouse } from '../../api/order';
import { AfterSaleOrderDetailMixins } from '../../../behaviors/page';
const { showLoading, hideLoading, tipError } = require('../../../common/env');

Page({
    mixins: [AfterSaleOrderDetailMixins],
    data: {
        orderType: ORDER_TYPE,
        afterSaleOrderDetail: {},
        afterSaleOrderStatusInfo: {},
        showRefuseModal: false, // 拒绝弹窗
        activityUrl: '',
    },
    onLoad: async function (options) {
        const { orderNumber } = options;
        await this.refreshAfterOrderDetail(orderNumber);
    },
    refreshAfterOrderDetail: async function (orderNumber) {
        showLoading('加载中...');
        console.log('BrandAfterSaleDetail => 售后单号为：', orderNumber);
        const afterSaleOrderDetail = await getBrandAfterSaleOrderDetail(orderNumber);
        console.log('BrandAfterSaleDetail => 售后单详情为：', afterSaleOrderDetail);
        this.setData({
            afterSaleOrderDetail,
            afterSaleOrderStatusInfo: handleBrandAfterSaleOrderStatus(afterSaleOrderDetail),
            activityUrl: `${PACKAGE_ACTIVITY_PAGE['detail']}?activityId=${afterSaleOrderDetail.activityId}`,
        });
        hideLoading();
    },
    // 同意售后
    invokeAgreeAction: async function () {
        const { serialNumber } = this.data.afterSaleOrderDetail;
        const res = await dealBrandAfterSaleOrder(serialNumber, {
            isRefuse: false,
            refuseReason: '',
        });
        if (res) {
            await this.refreshAfterOrderDetail(serialNumber);
        }
    },
    // 拒绝售后
    invokeRefuseAction: async function () {
        this.setData({
            showRefuseModal: true,
        });
    },
    refuseModalAction: async function (event) {
        const refuseReason = event.detail;
        const { serialNumber } = this.data.afterSaleOrderDetail;
        if (!refuseReason) {
            tipError('拒绝原因不能为空');
            return;
        }
        const param = {
            isRefuse: true,
            refuseReason,
        };
        const res = await dealBrandAfterSaleOrder(serialNumber, param);
        if (res) {
            await this.refreshAfterOrderDetail(serialNumber);
        }
    },
    // 同意入库
    invokeAgreeWarehouseAction: async function () {
        const { serialNumber } = this.data.afterSaleOrderDetail;
        const res = await brandAgreeWearhouse(serialNumber);
        if (res) {
            await this.refreshAfterOrderDetail(serialNumber);
        }
    },
    // 拒绝入库
    invokeRefuseWarehouseAction: function () {
        this.setData({
            showRefuseWarehouseModal: true,
        });
    },
    refuseWarehouseModalAction: async function (event) {
        const refuseReason = event.detail;
        const { serialNumber } = this.data.afterSaleOrderDetail;
        if (!refuseReason) {
            tipError('拒绝原因不能为空');
            return;
        }
        const param = {
            refuseAfterSaleReason: refuseReason,
        };
        const res = await brandRefuseWearhouse(serialNumber, param);
        if (res) {
            await this.refreshAfterOrderDetail(serialNumber);
        }
    },
})